﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.Extensions.Options;
using RndMaterialDisposalService.Attrbutes;

namespace RndMaterialDisposal.Extensions.Authorization
{
    /// <summary>
    /// 实现动态添加策略
    /// </summary>
    [NAutowired(Lifecycle.Singleton)]
    public class PermissionAuthorizationPolicyProvider : DefaultAuthorizationPolicyProvider, IAuthorizationPolicyProvider
    {
        public PermissionAuthorizationPolicyProvider(IOptions<AuthorizationOptions> options) : base(options) { }

        public new Task<AuthorizationPolicy> GetDefaultPolicyAsync()
        {
            return base.GetDefaultPolicyAsync(); 
        }

        public new Task<AuthorizationPolicy?> GetFallbackPolicyAsync()
        {
            return base.GetFallbackPolicyAsync();
        }
        

        public new Task<AuthorizationPolicy?> GetPolicyAsync(string policyName)
        {
            //if (policyName.StartsWith(UserPermission.User))
            //{
                var policyBuilder = new AuthorizationPolicyBuilder("Bearer");
                policyBuilder.AddRequirements(new PermissionAuthorizationRequirement(policyName));
                return Task.FromResult<AuthorizationPolicy?>(policyBuilder.Build());
            //}
            //return base.GetPolicyAsync(policyName);
        }
    }
}
